package org.lakesidecdc.outreach.client;

import java.util.ArrayList;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.VerticalPanel;

public class EventList extends Composite {

	private VerticalPanel outer = new VerticalPanel();
	private FlexTable table = new FlexTable();
	private Button addEventButton = new Button("+");
	private AddEventPanel addEventPanel = new AddEventPanel();
	private ArrayList<ProjectEvent> events = new ArrayList<ProjectEvent>();
	
	public EventList() {
		
		// Set up table columns
		table.setText(0, 0, "Date");
		table.setText(0, 1, "Volunteer Hours");
		table.setText(0, 2, "Staff Hours");
		table.setText(0, 3, "Expenditures");
		
		outer.add(table);
		outer.add(addEventButton);
		outer.setStyleName("eventList");
		
		initWidget(outer);
		
		// Listen for click events
		addEventButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				addEventPanel.show();
			}
		});
		
	}

	public float getTotalVolunteerHours() {
		float total = 0;
		for(ProjectEvent event: events) {
			total += event.getVolunteerHours();
		}
		return total;
	}

	public float getTotalStaffHours() {
		float total = 0;
		for(ProjectEvent event: events) {
			total += event.getStaffHours();
		}
		return total;
	}

	public double getTotalCost() {
		double total = 0;
		for(ProjectEvent event: events) {
			total += event.getCost();
		}
		return total;
	}
	
	public void add(ProjectEvent event) {
		events.add(event);
		update();
	}
	
	public boolean contains(ProjectEvent event) {
		return events.contains(event);
	}
	
	public void update() {
		int row = 1;
		for(ProjectEvent event: events) {
			table.setText(row, 0, event.getDate().toString());
			table.setText(row, 1, Float.toString(event.getVolunteerHours()));
			table.setText(row, 2, Float.toString(event.getStaffHours()));
			table.setText(row, 3, Float.toString(event.getCost()));
			row++;
		}
	}
}
